//
//  MM_CodeResultInfoCell.m
//  MallManage_IOS
//
//  Created by 张浩 on 2023/10/16.
//

#import "MM_CodeResultInfoCell.h"

@interface MM_CodeResultInfoCell ()

@property (nonatomic, strong) UIView *baseView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *descLabel;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIButton *descBtn;
@property (nonatomic, strong) UIButton *infoBtn;

@end

@implementation MM_CodeResultInfoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        _baseView = [[UIView alloc] init];
        _baseView.backgroundColor = MM_ColorUtil(@"#ffffff");
        [self.contentView addSubview:_baseView];
        
        _nameLabel = [Custom_ZHTool uiLabelCreate:@"" Font:FONT(14) TextColor:MM_ColorUtil(@"#333333")];
        [self.baseView addSubview:_nameLabel];
        
        _descLabel = [Custom_ZHTool uiLabelCreate:@"" Font:FONT(14) TextColor:MM_ColorUtil(@"#333333")];
        [self.baseView addSubview:_descLabel];
        
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = MM_ColorUtil(@"#f7f7f7");
        [self.baseView addSubview:_lineView];
        
        _descBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_descBtn setBackgroundColor:[UIColor redColor]];
        [_descBtn setImage:MM_Image(@"bianjiImage") forState:UIControlStateNormal];
        [_descBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        _descBtn.tag = 100;
        [self.baseView addSubview:_descBtn];

        _infoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_descBtn setBackgroundColor:[UIColor redColor]];
        [_infoBtn setImage:MM_Image(@"skuWenhaoImage") forState:UIControlStateNormal];
        [_infoBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        _infoBtn.tag = 101;
        [self.baseView addSubview:_infoBtn];

        [self setUpViews];
    }
    return self;
}

- (void)setUpViews {
      
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.top.bottom.equalTo(self.contentView);
        make.height.equalTo(44);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(12);
        make.centerY.equalTo(self.baseView);
    }];
    
    [self.infoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.nameLabel.mas_right).offset(5);
        make.centerY.equalTo(self.baseView);
        make.width.height.equalTo(20);
    }];
                                                                                  
    [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(-12);
        make.centerY.equalTo(self.baseView);
    }];
    
    [self.descBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.equalTo(-12);
        make.centerY.equalTo(self.baseView);
        make.width.height.equalTo(20);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.equalTo(self.baseView);
        make.bottom.equalTo(0);
        make.height.equalTo(1);
    }];
}

- (void)setModel:(MM_CodeResultListModel *)model {
    _model = model;
    self.descBtn.hidden = YES;
    self.infoBtn.hidden = YES;
    self.descLabel.textColor = MM_ColorUtil(@"#333333");
    
    self.nameLabel.text = model.nameStr;
    self.descLabel.text = model.descStr;
                              
    CGSize nameSize = [Custom_ZHTool getTextSizeWithFont:self.nameLabel.font text:self.nameLabel.text];
    
    [self.nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.width.equalTo(nameSize.width + 5);
    }];
    
    [self.descLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(-12);
    }];
    
    if ([model.nameStr isEqualToString:@"商城价"] || [model.nameStr isEqualToString:@"控价"]) {
        self.infoBtn.hidden = NO;
        
    }
    
    if ([model.nameStr isEqualToString:@"旧编码"] || [model.nameStr isEqualToString:@"控价"]) {
        self.descBtn.hidden = NO;
        self.descLabel.textColor = MM_ColorUtil(@"#1F71C4");
                           
        [self.descLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.right.equalTo(-45);
        }];
    }
}

- (void)btnClick:(UIButton *)btn {
    
    NSInteger index = btn.tag - 100;
    
    if (self.changeBlock) {
        self.changeBlock(self.model,index);
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
